package com.weirq.mvc.login;

import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.weirq.dao.impl.BaseDaoImpl;
import com.weirq.mvc.BaseController;
import com.weirq.util.BaseUtils;
import com.weirq.util.MD5Util;
import com.weirq.vo.UserModel;

/**
 * 
 * @author pc-Haitao
 * @ClassName:LoginController 登录注册控制器
 * @Version 版本
 * @Modifiedby 修改人
 * @Copyright 公司名称
 * @date 2016年8月18日下午4:45:21
 */
@Controller
@RequestMapping("/")
public class LoginController extends BaseController {
	/**
	 * 用户登录
	 * 
	 * @param userName
	 * @param pwd
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("login")
	public String login(String userName, String pwd, HttpSession session) throws Exception {
		BaseDaoImpl baseDaoImpl = new BaseDaoImpl();
		if (baseDaoImpl.find(userName, pwd).size() > 0) {
			if (baseDaoImpl.find(userName, pwd).get(0).get("status").hashCode() == 0) // 验证用户激活状态
			{
				JOptionPane.showMessageDialog(null, "邮箱未激活无法登录!", "错误信息", JOptionPane.ERROR_MESSAGE);
				return "redirect:login.jsp";
			} else {
				session.setAttribute("username", userName);
				session.setAttribute("email", baseDaoImpl.find(userName, pwd).get(0).get("email"));
				return "redirect:index.jsp";
			}
		} else {// 弹出信息框提示
			JOptionPane.showMessageDialog(null, "帐户名或密码错误!", "错误信息", JOptionPane.ERROR_MESSAGE);
			return "redirect:login.jsp";
		}
	}

	/**
	 * 用户 退出
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("logout")
	public String logout(HttpSession session) {
		if (session != null) {
			session.invalidate();// 注销 调用该方法 会清空所有已定义的session对象 而不是值
		}
		return "redirect:login.jsp";
	}

	/**
	 * 注册用户
	 * 
	 * @param username
	 * @param password
	 * @param email
	 * @return
	 * @throws Exception
	 */
	@ResponseBody //将内容或对象作为 HTTP 响应正文返回，使用@ResponseBody将会跳过视图处理部分，而是调用适合HttpMessageConverter，将返回值写入输出流。
	@RequestMapping("reg")
	public String reg(String username, String password, String email) throws Exception {
		BaseDaoImpl baseDaoImpl = new BaseDaoImpl();
		if (baseDaoImpl.findbyname(username).size() > 0) {
			JOptionPane.showMessageDialog(null, "帐户名已存在,请重新输入!", "错误信息", JOptionPane.ERROR_MESSAGE);
			return "fail";
		} else {
			if (baseDaoImpl.findbyemail(email).size() > 0) {
				JOptionPane.showMessageDialog(null, "邮箱已存在,请重新输入!", "错误信息", JOptionPane.ERROR_MESSAGE);
				return "error";
			} else if (!BaseUtils.isNotEmpty(username) && !BaseUtils.isNotEmpty(password) && !BaseUtils.isNotEmpty(email)) {
				UserModel userModel = new UserModel();
				userModel.setId(UUID.randomUUID().toString());// 使用uuid生成的32位id
				userModel.setUsername(username);
				userModel.setPassword(password);
				userModel.setEmail(email);
				userModel.setStatus(0);
				userModel.setValidateCode(MD5Util.encode2hex(email));// 使用md5加密邮箱生成的激活码
				userModel.setRegisterTime(new Date());
				baseDaoImpl.save(userModel);
				JOptionPane.showMessageDialog(null, "恭喜你注册成功！请到注册的邮箱点击链接激活！", "注册成功", JOptionPane.PLAIN_MESSAGE);
				hdfsDB.mkdir("/" + username);// 在hadoop上创建次此注册用户名的文件夹
				// /邮件的内容
				StringBuffer sb = new StringBuffer("点击下面链接激活账号，48小时生效，否则重新注册账号，链接只能使用一次，请尽快激活！<br>");
				sb.append("<a href=\"http://localhost:8081/CloudDisk/user/register.do?email=");
				sb.append(email);
				sb.append("&validateCode=");
				sb.append(userModel.getValidateCode());
				sb.append("\">请点击此链接进行邮箱激活!");
				sb.append("</a>");
				// 发送邮件
				SendEmail.send(email, sb.toString());
			}
		}
		return "redirect:login.jsp";
	}

	/**
	 * 找回密码 修改密码
	 * 
	 * @param email
	 * @param password
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("reset")
	public String reset(String email, String password) throws Exception {
		BaseDaoImpl baseDaoImpl = new BaseDaoImpl();
		if (baseDaoImpl.findbyemail(email).size() > 0) {
			if (password.equals(baseDaoImpl.findbyemail(email).get(0).get("password").toString())) {
				JOptionPane.showMessageDialog(null, "新密码与原密码相同,请重新输入!", "错误信息", JOptionPane.ERROR_MESSAGE);
				return "fail";
			} else if (!BaseUtils.isNotEmpty(password) && !BaseUtils.isNotEmpty(email)) {
				baseDaoImpl.updatePassword(email, password);
			}
		} else {
			JOptionPane.showMessageDialog(null, "邮箱不存在,请重新输入!", "错误信息", JOptionPane.ERROR_MESSAGE);
			return "error";
		}
		return "redirect:login.jsp";
	}
}
